home *** CD-ROM | disk | FTP | other *** search
- Path: news.compuserve.com!newsmaster
- From: RossBoylan@aol.com (Ross Boylan)
- Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc
- Subject: Virtual function question
- Date: Mon, 01 Apr 1996 18:17:56 GMT
- Organization: CompuServe Incorporated
- Message-ID: <4jp6e9$ou5@dub-news-svc-1.compuserve.com>
- NNTP-Posting-Host: ad30-253.compuserve.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- I am trying to define, a la Smalltalk, a mix-in which defines the
- various comparison operators in terms of one fundamental operator, <.
-
-
- typedef int bool;
- class RBMagnitude {
- public:
- //subclass should redefine < with appropriate types
- virtual bool operator<(const RBMagnitude&) const= 0;
- virtual bool operator>(const RBMagnitude& x) const {return *this <
- x;};
- // and many more
- };
-
- class A : public RBMagnitude {
- public:
- virtual bool operator<(const A& a) const {return (this->n < a.n);};
- private:
- int n;
- };
-
-
- class A test;
-
- This fails in MSVC++ 4.0 with the error that A is virtual because the
- operator<(const RBMagnitude&) is not defined.
- It looks to me as if this is because the operator< for A has a
- different type signature (it uses an A rather than an RBMagnitude).
- If so, the problem is the language definition, not Microsoft's
- implementation.
-
- 1) Is this interpretation correct?
- 2) Is there any way around this problem? Commenting out the
- definition of RBMagnitude::operator< doesn't work, because
- RBMagnitude::operator> requires it. I suppose I could define a bogus
- operator< which throws an exception, but this seems awkward (in
- particular, if A and B are both RBMagnitudes and I ask A<B, I fail at
- run time rather than compile time).
-
- I would appreciate e-mail; I'll summarize here.
-
-